import matplotlib.pyplot as plt
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

# 准备数据
X = np.array([[1, 2], [2, 3], [3, 4], [4, 6], [5, 7], [6, 8]])
y = np.array([0, 0, 0, 1, 1, 1])

# 应用LDA转换
lda = LinearDiscriminantAnalysis(n_components=1)
X_lda = lda.fit_transform(X, y)

# 可视化原始数据
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis', marker='o')
plt.title('Original Data')
plt.xlabel('x1')
plt.ylabel('x2')

# 可视化转换后的数据
plt.subplot(1, 2, 2)
plt.scatter(X_lda, [0] * len(X_lda), c=y, cmap='viridis', marker='o')
plt.title('LDA Transformed Data')
plt.xlabel('LDA Component')
plt.yticks([])

plt.tight_layout()
plt.show()
